package com.ruoyi.web.controller.net;

import com.ruoyi.net.domain.ElementIsisPort;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.IElementIsisPortService;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.ElementIsisPortIp;
import com.ruoyi.net.service.IElementIsisPortIpService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网元-ISIS接口配置-ipv4地址族Controller
 * 
 * @author net
 * @date 2024-11-17
 */
@Api(tags = "网元-ISIS接口配置-ipv4地址族")
@RestController
@RequestMapping("/element/isis/port/ip")
public class ElementIsisPortIpController extends BaseController {
    @Autowired
    private IElementIsisPortIpService elementIsisPortIpService;

    @Autowired
    private IElementIsisPortService elementIsisPortService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 查询网元-ISIS接口配置-ipv4地址族列表
     */
    @ApiOperation(value = "查询网元-ISIS接口配置-ipv4地址族列表", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:ip:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElementIsisPortIp elementIsisPortIp) {
        startPage();
        List<ElementIsisPortIp> list = elementIsisPortIpService.selectElementIsisPortIpList(elementIsisPortIp);
        return getDataTable(list);
    }

    /**
     * 导出网元-ISIS接口配置-ipv4地址族列表
     */
    @ApiOperation(value = "导出网元-ISIS接口配置-ipv4地址族列表", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:ip:export')")
    @Log(title = "网元-ISIS接口配置-ipv4地址族", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElementIsisPortIp elementIsisPortIp) {
        List<ElementIsisPortIp> list = elementIsisPortIpService.selectElementIsisPortIpList(elementIsisPortIp);
        ExcelUtil<ElementIsisPortIp> util = new ExcelUtil<ElementIsisPortIp>(ElementIsisPortIp.class);
        util.exportExcel(response, list, "网元-ISIS接口配置-ipv4地址族数据");
    }

    /**
     * 获取网元-ISIS接口配置-ipv4地址族详细信息
     */
    @ApiOperation(value = "获取网元-ISIS接口配置-ipv4地址族详细信息", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:ip:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(elementIsisPortIpService.selectElementIsisPortIpById(id));
    }

    /**
     * 新增网元-ISIS接口配置-ipv4地址族
     */
    @ApiOperation(value = "新增网元-ISIS接口配置-ipv4地址族", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:ip:add')")
    @Log(title = "网元-ISIS接口配置-ipv4地址族", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElementIsisPortIp elementIsisPortIp) {
        sendXml(elementIsisPortIp);
        return toAjax(elementIsisPortIpService.insertElementIsisPortIp(elementIsisPortIp));
    }

    /**
     * 修改网元-ISIS接口配置-ipv4地址族
     */
    @ApiOperation(value = "修改网元-ISIS接口配置-ipv4地址族", httpMethod = "PUT")
    //    @PreAuthorize("@ss.hasPermi('system:ip:edit')")
    @Log(title = "网元-ISIS接口配置-ipv4地址族", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElementIsisPortIp elementIsisPortIp) {
        sendXml(elementIsisPortIp);
        return toAjax(elementIsisPortIpService.updateElementIsisPortIp(elementIsisPortIp));
    }

    /**
     * 删除网元-ISIS接口配置-ipv4地址族
     */
    @ApiOperation(value = "删除网元-ISIS接口配置-ipv4地址族", httpMethod = "DELETE")
    //    @PreAuthorize("@ss.hasPermi('system:ip:remove')")
    @Log(title = "网元-ISIS接口配置-ipv4地址族", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(elementIsisPortIpService.deleteElementIsisPortIpByIds(ids));
    }

    private void sendXml(ElementIsisPortIp entity) {
        // TODO: 2024/12/1  设备联调，添加并激活
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getElementId());
            deviceManager.getDevice(netElement.getIp());
            String operation = "create";
            if ("0".equals(entity.getStatus())) {
                operation = "remove";
            }
            ElementIsisPort elementIsisPort = elementIsisPortService.selectElementIsisPortById(entity.getIsisId());
            String xml = "<isis xmlns=\"http://fiberhome.com/fhnw/yang/ipprotocol/routing/fos-isis\">" +
                    "    <isis-instances>" +
                    "      <isis-instance>" +
                    "        <instance-name>" + elementIsisPort.getIsis() + "</instance-name>" +
                    "        <isis-interfaces>" +
                    "          <isis-interface nc:operation=\"" + operation + "\">" +
                    "            <interface-name>" + elementIsisPort.getPortName() + "</interface-name>" +
                    // -------start IPV4地址族 -------
                    "            <interface-af>" +
                    "              <af-name>ipv4</af-name>" +
                    "              <saf-name>" + entity.getSingleOrMulti() + "</saf-name>" +
                    "              <prefix-sids>" +
//                    "                <type>" + entity.getSrConfig().split("|")[0] + "</type>" +
//                    "                <absolute-id>" + entity.getSrConfig().split("|")[1] + "</absolute-id>" +
//                    "                <tyindex-idpe>" + entity.getSrConfig().split("|")[2] + "</index-id>" +
                    "              </prefix-sids>" +
                    "              <bfd-isis-if xmlns=\"http://fiberhome.com/fhnw/yang/oam/fos-bfd\">" +
                    "                <bfd-enable>" + entity.getBfdEnable() + "</bfd-enable>" +
                    "                <time-interval>" + entity.getBfdAttribute() + "</time-interval>" +
                    "              </bfd-isis-if>" +
                    "            </interface-af>" +
                    // -------end IPV4地址族 -------
                    "          </isis-interface>" +
                    "        </isis-interfaces>" +
                    "      </isis-instance>" +
                    "    </isis-instances>" +
                    "  </isis>";
            System.out.println(xml);
            deviceManager.editConfig(xml, "merge");
        }
    }
}